using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;


namespace SIS.DAL {
/// <summary>
/// The FamilyMemberContactTableDB class is responsible for interacting with the database to retrieve and store information
/// about FamilyMemberContactTable objects.
/// </summary>
public class FamilyMemberContactTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of FamilyMemberContactTable from the underlying datasource.
/// </summary>
/// <param name="familyMemberContactKey">The unique FamilyMemberContactKey of the FamilyMemberContactTable in the database.</param>
/// <returns>An FamilyMemberContactTable when the FamilyMemberContactKey was found in the database, or null otherwise.</returns>
public static FamilyMemberContactTable GetItem(int familyMemberContactKey)
{
FamilyMemberContactTable myFamilyMemberContactTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberContactTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@familyMemberContactKey", familyMemberContactKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myFamilyMemberContactTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myFamilyMemberContactTable;
}
}

public static FamilyMemberContactTable GetItemByUserAccountKey(int UserAccountKey)
{
    FamilyMemberContactTable myFamilyMemberContactTable = null;
    using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
    {
        using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberContactSelectSingleItemByUserAccountKey", myConnection))
        {
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.AddWithValue("@UserAccountKey", UserAccountKey);

            myConnection.Open();
            using (SqlDataReader myReader = myCommand.ExecuteReader())
            {
                if (myReader.Read())
                {
                    myFamilyMemberContactTable = FillDataRecord(myReader);
                }
                myReader.Close();
            }
            myConnection.Close();
        }
        return myFamilyMemberContactTable;
    }
}

/// <summary>
/// Returns a list with FamilyMemberContactTable objects.
/// </summary>
/// <returns>A generics List with the FamilyMemberContactTable objects.</returns>
public static FamilyMemberContactTableList GetList()
{
FamilyMemberContactTableList tempList = new FamilyMemberContactTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberContactTableSelectList", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with FamilyMemberContactTable objects.
/// </summary>
/// <returns>A generics List with the FamilyMemberContactTable objects.</returns>
public static FamilyMemberContactTableList GetListByUserAccountKey(int UserAccountKey)
{
FamilyMemberContactTableList tempList = new FamilyMemberContactTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberContactTableSelectListByUserAccountKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a FamilyMemberContactTable in the database.
/// </summary>
/// <param name="myFamilyMemberContactTable">The FamilyMemberContactTable instance to save.</param>
/// <returns>The new FamilyMemberContactKey if the FamilyMemberContactTable is new in the database or the existing FamilyMemberContactKey when an item was updated.</returns>
public static int Save(FamilyMemberContactTable myFamilyMemberContactTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberContactTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myFamilyMemberContactTable.FamilyMemberContactKey == -1)
{myCommand.Parameters.AddWithValue("@familyMemberContactKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@familyMemberContactKey", myFamilyMemberContactTable.FamilyMemberContactKey);
}
if (myFamilyMemberContactTable.UserAccountKey == null){
myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountKey", myFamilyMemberContactTable.UserAccountKey);
}
if (string.IsNullOrEmpty(myFamilyMemberContactTable.MobileNumber))
{
myCommand.Parameters.AddWithValue("@mobileNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@mobileNumber", myFamilyMemberContactTable.MobileNumber);
}
if (string.IsNullOrEmpty(myFamilyMemberContactTable.HomePhoneNumber))
{
myCommand.Parameters.AddWithValue("@homePhoneNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@homePhoneNumber", myFamilyMemberContactTable.HomePhoneNumber);
}
if (string.IsNullOrEmpty(myFamilyMemberContactTable.OfficePhoneNumber))
{
    myCommand.Parameters.AddWithValue("@OfficePhoneNumber", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@OfficePhoneNumber", myFamilyMemberContactTable.OfficePhoneNumber);
}
if (string.IsNullOrEmpty(myFamilyMemberContactTable.FaxNumber))
{
    myCommand.Parameters.AddWithValue("@FaxNumber", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@FaxNumber", myFamilyMemberContactTable.FaxNumber);
}
if (string.IsNullOrEmpty(myFamilyMemberContactTable.OtherNumber))
{
myCommand.Parameters.AddWithValue("@otherNumber", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@otherNumber", myFamilyMemberContactTable.OtherNumber);
}
if (string.IsNullOrEmpty(myFamilyMemberContactTable.EmailAddress))
{
myCommand.Parameters.AddWithValue("@emailAddress", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@emailAddress", myFamilyMemberContactTable.EmailAddress);
}
if (myFamilyMemberContactTable.Status == null)
{
    myCommand.Parameters.AddWithValue("@Status", DBNull.Value);
}
else
{
    myCommand.Parameters.AddWithValue("@Status", myFamilyMemberContactTable.Status);
}
if (myFamilyMemberContactTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myFamilyMemberContactTable.CreatedDateTime);
}
if (myFamilyMemberContactTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myFamilyMemberContactTable.ModifiedDateTime);
}

DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a FamilyMemberContactTable from the database.
/// </summary>
/// <param name="familyMemberContactKey">The FamilyMemberContactKey of the FamilyMemberContactTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(int familyMemberContactKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocFamilyMemberContactTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@familyMemberContactKey", familyMemberContactKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the FamilyMemberContactTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static FamilyMemberContactTable FillDataRecord(IDataRecord myDataRecord )
{
FamilyMemberContactTable myFamilyMemberContactTable = new FamilyMemberContactTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FamilyMemberContactKey")))
{
myFamilyMemberContactTable.FamilyMemberContactKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("FamilyMemberContactKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
{
myFamilyMemberContactTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("MobileNumber")))
{
myFamilyMemberContactTable.MobileNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("MobileNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("HomePhoneNumber")))
{
myFamilyMemberContactTable.HomePhoneNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("HomePhoneNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OfficePhoneNumber")))
{
    myFamilyMemberContactTable.OfficePhoneNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("OfficePhoneNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FaxNumber")))
{
    myFamilyMemberContactTable.FaxNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("FaxNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OtherNumber")))
{
myFamilyMemberContactTable.OtherNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("OtherNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("EmailAddress")))
{
myFamilyMemberContactTable.EmailAddress = myDataRecord.GetString(myDataRecord.GetOrdinal("EmailAddress"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
    myFamilyMemberContactTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myFamilyMemberContactTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myFamilyMemberContactTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
return myFamilyMemberContactTable;
}
}

 } 
